package com.educate.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.educate.aclservice.entity.AclUser;
import com.educate.aclservice.service.AclRoleService;
import com.educate.aclservice.service.AclUserService;
import com.educate.commonutils.MD5;
import com.educate.commonutils.ResultOne;
import com.educate.servicebase.exceptionhander.EducateException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author educate.wht
 * @since 2022-09-02
 */
@RestController
@RequestMapping("/admin/acl/user")
@CrossOrigin
public class AclUserController {
    @Autowired
    private AclUserService aclUserService;

    @Autowired
    private AclRoleService roleService;
//
    @PostMapping("login")
    public ResultOne login(@RequestBody(required = false) AclUser aclUser){
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        String username = aclUser.getUsername();
        wrapper.eq("username",username);
        AclUser user = aclUserService.getOne(wrapper);
        if(user==null){
            return ResultOne.error().data("用户不存在",null);
        }
        if(!user.getPassword().equals(MD5.encrypt(aclUser.getPassword()))){
            return ResultOne.error().data("密码错误",null);
        }
        return ResultOne.ok().data("登录成功","账户密码正确");
    }

    @ApiOperation(value = "获取管理用户分页列表")
    @PostMapping("pageUserCondition/{current}/{limit}")
    public ResultOne index(@PathVariable long current, @PathVariable long limit,
                           @RequestBody(required = false) AclUser userQueryVo) {
        //创建page对象
        Page<AclUser> pageUser = new Page<>(current,limit);
        //构建条件
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        String name=userQueryVo.getUsername();
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("username",name);
        }
        aclUserService.page(pageUser,wrapper);
        long total = pageUser.getTotal();//总记录数目
        List<AclUser> records = pageUser.getRecords();//数据list集合
        return ResultOne.ok().data("total",total).data("rows",records);
    }
    //根据id获取管理用户
    @ApiOperation(value = "根据id获取管理用户")
    @GetMapping("get/{id}")
    public ResultOne get(@PathVariable String id){
        AclUser user = aclUserService.getById(id);
        return ResultOne.ok().data("user",user);
    }

    @ApiOperation(value = "新增管理用户")
    @PostMapping("save")
    public ResultOne save(@RequestBody AclUser user) {
        user.setPassword(MD5.encrypt(user.getPassword()));
        aclUserService.save(user);
        return ResultOne.ok();
    }

    @ApiOperation(value = "修改管理用户")
    @PutMapping("update")
    public ResultOne updateById(@RequestBody AclUser user) {
        aclUserService.updateById(user);
        return ResultOne.ok();
    }

    @ApiOperation(value = "删除管理用户")
    @DeleteMapping("remove/{id}")
    public ResultOne remove(@PathVariable String id) {
        aclUserService.removeById(id);
        return ResultOne.ok();
    }

    @ApiOperation(value = "根据id列表删除管理用户")
    @DeleteMapping("batchRemove")
    public ResultOne batchRemove(@RequestBody List<String> idList) {
        aclUserService.removeByIds(idList);
        return ResultOne.ok();
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public ResultOne toAssign(@PathVariable String userId) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(userId);
        return ResultOne.ok().data(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public ResultOne doAssign(@RequestParam String userId,@RequestParam String[] roleId) {
        roleService.saveUserRoleRealtionShip(userId,roleId);
        return ResultOne.ok();
    }



}

